package gui.renderers;

import gui.DBMS_Client;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import dto.ColumnIconDTO;
import dto.DatabaseIconDTO;
import dto.TableIconDTO;

public class DBMSTreeCellRenderer extends DefaultTreeCellRenderer {

	public DBMSTreeCellRenderer() {
		super();
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

		super.getTreeCellRendererComponent(tree, value, selected, expanded,
				leaf, row, hasFocus);

		// set normal font to all tree cells
		setFont(getFont().deriveFont(Font.PLAIN));

		Object obj = ((DefaultMutableTreeNode) value).getUserObject();
		if (obj instanceof DatabaseIconDTO) {
			ImageIcon dbicon = new ImageIcon(
					DBMS_Client.class.getResource("images/database-mini.png"));
			setIcon(dbicon);
			setText(((DatabaseIconDTO) obj).getDatabaseName());
			if (((DatabaseIconDTO) obj).isSetAsDefault()) {
				setFont(getFont().deriveFont(Font.BOLD));
				setTextSelectionColor(Color.BLUE);
			} else {
				setFont(getFont().deriveFont(Font.PLAIN));
			}
		} else if (obj instanceof TableIconDTO) {
			ImageIcon tableicon = new ImageIcon(
					DBMS_Client.class.getResource("images/table-mini.png"));
			setIcon(tableicon);
			setText(((TableIconDTO) obj).getTableName());
		} else if (obj instanceof ColumnIconDTO) {
			ImageIcon colIcon = new ImageIcon(
					DBMS_Client.class.getResource("images/column-mini-icon.png"));
			setIcon(colIcon);
			setText(((ColumnIconDTO) obj).getColName() + " ("
					+ ((ColumnIconDTO) obj).getDataType() + ")");
		}
		return this;
	}
}
